<template>
	<view class="free-to-buy">
		<!-- 顶部图片 -->
		<view class="free-top">
			<view class="active-gz" @click="openRules">活动规则</view>
			<view class="active-goods-box" v-if="detail.img">
				<view class="active-goods-left"><image :src="IMG_URL + detail.img" mode="aspectFill"></image></view>
				<view class="active-goods-right">
					<view class="active-goods-yq">已邀请{{ detail.fans }}人，还差{{ detail.left_fans }}人</view>
					<view class="active-goods-time">
						<uni-countdown
							v-if="timeShow"
							splitorColor="#fa8384"
							color="#FFFFFF"
							background-color="#fa8384"
							:day="times[0]"
							:hour="times[1]"
							:minute="times[2]"
							:second="times[3]"
						></uni-countdown>
						后结束
					</view>
					<button class="active-goods-fx" open-type="share">继续分享</button>
				</view>
			</view>
		</view>
		<!-- 活动列表 -->
		<view class="active-box">
			<view class="active-title">活动列表</view>
			<active-list :list="list" @activeDetail="activeDetail" />
		</view>
		<!-- 加载更多 -->
		<uni-load-more :status="more"></uni-load-more>
		<!-- 活动规则 -->
		<activity-rules-popup ref="activityRulesPopup" />
	</view>
</template>

<script>
const app = getApp();
import activeList from './components/active-list/index.vue';
import activityRulesPopup from './components/activity-rules-popup/index.vue';
import { freeCat, freeCatOne } from '@/api/active.js';
import { xShare } from '@/api/index.js';
import countDown from './countDown.js';
export default {
	components: {
		activeList,
		activityRulesPopup
	},
	data() {
		return {
			id: '',
			page: 1,
			total: 0,
			list: [],
			status: 'loading',
			IMG_URL: app.globalData.IMG_URL,
			detail: {},
			timeShow: true,
			times: [] //倒计时
		};
	},
	onLoad(options) {
		this.id = options.id;
		if (app.globalData.openid) {
			this.freeCat(); // 猫咪列表
			this.freeCatOne(); // 获取活动详情
			this.xShare(options.openid);
		} else {
			app.getOpenId().then(res => {
				this.freeCat();
				this.freeCatOne();
				this.xShare(options.openid);
			});
		}
	},
	methods: {
		// 猫咪列表
		async freeCat() {
			this.status = 'loading';
			const res = await freeCat({
				openid: app.globalData.openid,
				page: this.page
			});
			this.total = res.total;
			this.list = this.list.concat(res.list);
			if (this.page == this.total || this.total == 0) {
				this.status = 'noMore';
			} else {
				this.status = 'more';
			}
		},
		// 获取活动详情
		async freeCatOne() {
			const res = await freeCatOne({
				openid: app.globalData.openid,
				id: this.id
			});
			this.detail = res;
			this.times = countDown(res.left_time * 1000);
			this.timeShow = true;
		},
		// 点击切换不同的活动详情
		activeDetail(id) {
			this.id = id;
			this.timeShow = false;
			this.freeCatOne();
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 300
			});
		},
		// 分享接口
		async xShare(openid) {
			if (openid) {
				const res = await xShare({
					type: 7,
					goods_id: this.id,
					openid: openid,
					openid_in: app.globalData.openid
				});
			}
		},
		// 查看活动规则
		openRules() {
			this.$refs.activityRulesPopup.open();
		}
	},
	// 上拉加载
	onReachBottom() {
		if (this.page == this.total) {
			return false;
		}
		this.page += 1;
		this.freeCat();
	},
	// 分享
	onShareAppMessage() {
		return {
			title: 'nabercat-life',
			path: `/pages/freeToBuy/freeToBuyDetail?openid=${app.globalData.openid}&id=${this.id}`
		};
	}
};
</script>

<style scoped lang="scss">
.free-to-buy {
	margin-bottom: 30rpx;
	.free-top {
		width: 100%;
		height: 578rpx;
		background: url(https://nabercat.oss-cn-hangzhou.aliyuncs.com/acat-xcx/active-top-img2.png) no-repeat;
		background-size: cover;
		position: relative;
		margin-bottom: 20rpx;
		padding: 234rpx 30rpx 28rpx;
		box-sizing: border-box;
		.active-gz {
			position: absolute;
			top: 170rpx;
			left: 0;
			width: 124rpx;
			height: 48rpx;
			background: #ffffff;
			border-radius: 0 28rpx 28rpx 0;
			text-align: center;
			line-height: 48rpx;
			font-size: 24rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #fa8384;
		}
		.active-goods-box {
			width: 100%;
			height: 100%;
			background: #ffffff;
			border-radius: 8rpx;
			padding: 30rpx;
			box-sizing: border-box;
			display: flex;
			.active-goods-left {
				width: 256rpx;
				height: 256rpx;
				margin-right: 32rpx;
				& > image {
					width: 100%;
					height: 100%;
					border-radius: 12rpx;
				}
			}
			.active-goods-right {
				flex: 1;
				display: flex;
				flex-direction: column;
				.active-goods-yq {
					font-size: 32rpx;
					font-family: PingFangSC-Semibold, PingFang SC;
					font-weight: 600;
					color: #171717;
					margin-bottom: 16rpx;
				}
				.active-goods-time {
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #fa8384;
					letter-spacing: 2rpx;
					display: flex;
					align-items: center;
					& > text {
						display: inline-block;
						width: 36rpx;
						height: 36rpx;
						background: #fa8384;
						border-radius: 4rpx;
						color: #ffffff;
						text-align: center;
						line-height: 36rpx;
						margin-right: 2rpx;
						&:nth-of-type(1) {
							margin-right: 4rpx;
						}
						&:nth-of-type(2) {
							margin-left: 4rpx;
						}
					}
				}
				.active-goods-fx {
					margin-top: auto;
					width: 100%;
					height: 60rpx;
					background: #fa8384;
					border-radius: 8rpx;
					line-height: 60rpx;
					font-size: 28rpx;
					font-family: PingFangSC-Semibold, PingFang SC;
					font-weight: 600;
					color: #ffffff;
					letter-spacing: 2rpx;
				}
			}
		}
	}
	.active-box {
		padding: 0 32rpx;
		.active-title {
			font-size: 32rpx;
			font-family: PingFangSC-Semibold, PingFang SC;
			font-weight: 600;
			color: #666666;
			margin-bottom: 24rpx;
			letter-spacing: 2rpx;
		}
	}
}
/deep/.uni-countdown__number {
	width: 36rpx !important;
	height: 36rpx !important;
	border-radius: 4rpx !important;
	text-align: center !important;
	line-height: 36rpx !important;
	font-size: 24rpx !important;
}
/deep/.uni-countdown__splitor {
	padding: 0 4rpx !important;
}
</style>
